<?php
namespace app\web\controller;

use app\model\Theme; 
use app\model\Fans; 

class Commshare extends Base
{
    public function Commshare()
    {
global $_G, $_P;
$system = $_G['system'];
$_G['page']['title'] = '参团列表 - ' . $system['sysname'];
//load_func('tpl');
$GLOBALS['frames'] = array();
$themeid = intval($_P['themeid']);
if($themeid <= 0){
	$themeid = Theme::getDefaultThemenew();
}
$theme = Theme::getThemeById($themeid);
if(empty($theme['id'])){
	return_msg('还没有创建活动,请联系管理员');
}
$config = 	Theme::getConfig($themeid);
$id = intval($_P['id']);
$uid = intval($_P['uid']);
$giftid = intval($_P['giftid']);
$do = 'comm';
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$url = createWebUrl('commshare',array('op' => 'display','themeid'=>$themeid));


$myinfo = Fans::getById($uid,$themeid);


// if($op == 'delete') {

// 	db_delete(t_commshare, array('id' => $id,'themeid'=>$themeid));

// 	$pm = array(":themeid"=>$themeid,":uid"=>$uid);

// 	return_msg('删除成功！', $url, 'success');
// }


if($op == 'display_old'){
	

	
	if (empty($_P['createtime']['start'])){
		$starttime =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
		$starttime =  strtotime(date('Y-m-d 00:00:00',$starttime));
	}else{
		$starttime =  strtotime($_P['createtime']['start']);
	}
	$endtime = empty($_P['createtime']['end']) ? strtotime(date('Y-m-d 23:59:59',TIMESTAMP)): strtotime($_P['createtime']['end']) ;
	

	$condition = " and 1=1 ";	
	$condition = " and ord.themeid = '{$themeid}' ";
	$condition.= " and ord.addtime > '{$starttime}' and ord.addtime <= '{$endtime}' ";

	if(!isset($_P['status'])){
		$status = -1;
	}else{
		$status = intval($_P['status']);
	}
	if($status >= 0){
		$condition.= " and ord.status = '{$status}'  ";
	}
	
	$keyword = $_P['keyword'];
	if(!empty($keyword)){
		$condition.= " and (f.nickname like '%{$keyword}%' or f.username like '%{$keyword}%' or f.mobile = '%{$keyword}%')";
	}

	if(!empty($uid)){
		$condition.= " and (ord.uid = {$uid} )";
	}

	$total = db_fetchcolumn ( 'SELECT COUNT(ord.id) FROM ' . tablename ( t_commshare ) . " as ord
			WHERE  1=1  {$condition} ORDER BY ord.id desc",$params);

	
	$sql = "SELECT ord.* FROM " . tablename ( t_commshare ) . " as ord
			left join  " . tablename ( t_fans ) . " as f on ord.uid = f.uid
						WHERE  1=1  {$condition} 
						      ORDER BY ord.id desc LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	$list = db_select ( $sql , $params);
	// foreach ($list as $index =>$row){

	// 		$fans = db_find ( "SELECT * FROM " . tablename ( t_fans ) . " WHERE uid = :uid and themeid =:themeid",array(":themeid"=>$themeid,":uid"=>$row['uid']));
	// 		$list[$index]['username'] = $fans['username'];
	// 		$list[$index]['mobile'] = $fans['mobile'];
	// }
	$pager = pagination ( $total, $pageNumber, $pageSize );

}


if($op == 'display'){
	$config['iscommshowfail']=1;
	$pm = array(":themeid"=>$themeid,":openid"=>$_P['openid']);
	$pu = array(":themeid"=>$themeid,":uid"=>intval($_P['uid']));



	$totalsuccess = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
	            WHERE m.themeid = :themeid and status=1 and m.openid = :openid',$pm);

	if ($config['iscommshowfail'] == 1){
		$totalfail = db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename (t_comm) . ' as m 
	            WHERE m.themeid = :themeid and status=0 and m.openid = :openid',$pm);
	}else{
		$totalfail=0;
	}
    //获取裂变红包未提现的金额
    if ($config['iscommshowwait'] == 1){
    	$commssum2 = floatval(db_fetchcolumn("SELECT sum(fee)  FROM ".tablename(t_commshare)."
			WHERE themeid = :themeid and openid = :openid and status = 0",$pm));
    	$commssum2 = round($commssum2,2);
    }else{
    	$commssum2=0;
    }
    $totalsuccess = round($totalsuccess,2);
    $totalfail = round($totalfail,2);
    $total = round($totalsuccess+$totalfail+$commssum2,2);

	if (empty($total) ){$total=0;}

	$pageNumber = max ( 1, intval ( $_P['page'] ) );
    if ($theme['endtime']<strtotime("-90 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben') {
            return_msg('活动结束超3个月,无法查看');
        }
        $pageSize = 20;
    }elseif ($theme['endtime']<strtotime("-30 day")) {
        if ($pageNumber>1 && $_G['username']!=='benben' && $_G['username']!=='caiwu' && $_G['username']!=='chuna') {
            return_msg('活动结束超1个月,无法查看');
        }
        $pageSize = 20;
    }else{
		$pageSize = 50;
	}
	
	$sql = "SELECT m.type,m.fee,s.fee,m.fromuid,m.uid,m.addtime FROM ".tablename(t_comm)." as m 
				INNER JOIN ".tablename(t_commshare)." as s on m.uid = s.uid
	            WHERE m.themeid = :themeid and m.status=1 and s.status<2 and m.openid = :openid
	            order by id desc
	            LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;


	if ($config['iscommshowfail'] == 0){
		$comm1txt=" and status=1 ";
	}
	// if ($config['iscommshowwait'] == 0){
	// 	$comm2txt=" and status=1 ";
	// }else{
	//	$comm2txt=" and status<2 ";
	// }
	if (!empty($uid)){
		$sql="SELECT * FROM (
			select type,fee,fromuid,uid,addtime,ip,city,msg,status,1 as comm
			from ".tablename(t_comm)."
			WHERE   themeid = :themeid and uid = :uid ".$comm1txt."

			union all
			select type,fee,uid,uid,addtime,ip,city,msg,status,2 as comm 
			from ".tablename(t_commshare)."
			WHERE   themeid = :themeid and  uid = :uid ".$comm2txt.") as t

			ORDER BY addtime desc,fee desc,comm
		    LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	}else{
		$sql="SELECT * FROM (
			select type,fee,fromuid,uid,addtime,ip,city,msg,status,1 as comm
			from ".tablename(t_comm)."
			WHERE   themeid = :themeid  ".$comm1txt."

			union all
			select type,fee,uid,uid,addtime,ip,city,msg,status,2 as comm 
			from ".tablename(t_commshare)."
			WHERE   themeid = :themeid  ".$comm2txt.") as t

			ORDER BY addtime desc,fee desc,comm
		    LIMIT " . ($pageNumber - 1) * $pageSize . ',' . $pageSize;
	}
	$list = db_select($sql,$pu);
	foreach ($list as $index => $row){
		$kehuinfo = Fans::getById($row['uid'],$themeid);
		if ($row['type']==4 || $row['type'] ==  7){
			$commfanslist['username']='';
			$commfanslist['avatar']='';
		}else{
			$commfanslist = Fans::getById($row['fromuid'],$themeid);
		}
        $list[$index]['time'] = date("Y-m-d H:i:s",$row['addtime']);
        $list[$index]['avatar2'] = $commfanslist['avatar'];
        $list[$index]['nickname2'] = mb_substr($commfanslist['username'],0,9,'utf-8');
        $list[$index]['avatar'] = $kehuinfo['avatar'];
        $list[$index]['nickname'] = mb_substr($kehuinfo['nickname'],0,9,'utf-8');
        if ($row['type'] ==  0) {
            $list[$index]['leixing'] = '开团红包';
        }else if ($row['type'] ==  1) {
            $list[$index]['leixing'] = '参团红包';
        }else if ($row['type'] ==  2) {
            $list[$index]['leixing'] = '二级分销';
        }else if ($row['type'] ==  3) {
            $list[$index]['leixing'] = '三级分销';
        }else if ($row['type'] ==  4 && $row['comm'] ==  1) {
            $list[$index]['leixing'] = '转发提现';
        }else if ($row['type'] ==  4 && $row['comm'] ==  2) {
            $list[$index]['leixing'] = '转发记录';
        }else if ($row['type'] ==  7 && $row['comm'] ==  1) {
            $list[$index]['leixing'] = '摇一摇提现';
        }else if ($row['type'] ==  7 && $row['comm'] ==  2) {
            $list[$index]['leixing'] = '摇一摇记录';
        }else if ($row['type'] ==  5) {
            $list[$index]['leixing'] = '阅读红包';
        }else if ($row['type'] ==  9) {
            $list[$index]['leixing'] = '单买红包';
        }else if ($row['type'] ==  10) {
            $list[$index]['leixing'] = '进入链接获得';
        }else if ($row['type'] ==  11) {
            $list[$index]['leixing'] = '自己报名获得';
        }else{
            $list[$index]['leixing'] = '参团红包';
        }
    }
	if (!empty($uid)){
	    $sql="SELECT COUNT(*) FROM (
			select type,fee,fromuid,uid,addtime,msg,status 
			from ".tablename(t_comm)."
			WHERE   themeid = :themeid and  uid = :uid

			union all
			select type,fee,uid,uid,addtime,msg,status 
			from ".tablename(t_commshare)."
			WHERE   themeid = :themeid and  uid = :uid and status<2) as t

			ORDER BY addtime desc,fee desc";

		$totala = db_fetchcolumn ($sql ,$pu);
	}

	if (!empty($uid)){
		$sql="SELECT COUNT(*) FROM (
			select type,fee,fromuid,uid,addtime,ip,city,msg,status,1 as comm
			from ".tablename(t_comm)."
			WHERE   themeid = :themeid and uid = :uid ".$comm1txt."

			union all
			select type,fee,uid,uid,addtime,ip,city,msg,status,2 as comm 
			from ".tablename(t_commshare)."
			WHERE   themeid = :themeid and  uid = :uid ".$comm2txt.") as t

			ORDER BY addtime desc,fee desc,comm";
			
		$totala = db_fetchcolumn ($sql ,$pu);
	}else{
		$sql="SELECT COUNT(*) FROM (
			select type,fee,fromuid,uid,addtime,ip,city,msg,status,1 as comm
			from ".tablename(t_comm)."
			WHERE   themeid = :themeid  ".$comm1txt."

			union all
			select type,fee,uid,uid,addtime,ip,city,msg,status,2 as comm 
			from ".tablename(t_commshare)."
			WHERE   themeid = :themeid  ".$comm2txt.") as t

			ORDER BY addtime desc,fee desc,comm";
			
		$totala = db_fetchcolumn ($sql ,$pu);
	}


	$pager = pagination ( $totala, $pageNumber, $pageSize );

}
$return_clicent = get_defined_vars();
return view('/commshare' , $return_clicent);
}
}